/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package guzik.lukasz.SnpLoginEngine;

import guzik.lukasz.SnpDataAPI.SnpDataAPI;
import guzik.lukasz.SnpLoginEngine.interfaces.LoginEngine;
import guzik.lukasz.SnpLoginEngine.interfaces.LoginEventListener;
import java.awt.Cursor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.ArrayList;
import org.openide.DialogDescriptor;
import org.openide.DialogDisplayer;
import org.openide.NotifyDescriptor;

/**
 *
 * @author Łukasz 'byku' Guzik
 */
public class LoginEngineImpl implements LoginEngine,
	ActionListener, KeyListener
{
    private static LoginEngineImpl instance = null;
    private boolean login = false;
    private ArrayList<LoginEventListener> loginListeners = new ArrayList<LoginEventListener>();
    private JLoginPanel loginPanel = null;
    private DialogDescriptor loginDescriptor = null;

    public LoginEngineImpl()
    {
    }

    public synchronized static LoginEngineImpl getInstance()
    {
	if(instance == null)
	{
	    instance = new LoginEngineImpl();
	}

	return instance;
    }

    public boolean isLogin()
    {
	return login;
    }

    @Override
    public void login()
    {
	if(login)
	{
	    logout();
	}

	loginPanel = new JLoginPanel();
	loginDescriptor = new DialogDescriptor(loginPanel, "Login",
		true, DialogDescriptor.OK_CANCEL_OPTION, null, this);

	loginDescriptor.setClosingOptions(new Object[]
		{
		});
	loginDescriptor.addPropertyChangeListener(new PropertyChangeListener()
	{
	    @Override
	    public void propertyChange(PropertyChangeEvent e)
	    {
		if(e.getPropertyName().equals(DialogDescriptor.PROP_VALUE)
			&& e.getNewValue() == DialogDescriptor.CLOSED_OPTION)
		{
		    loginDescriptor.setClosingOptions(null);
		}
	    }
	});

	loginPanel.jLoginField.addKeyListener(this);
	loginPanel.jPasswordField.addKeyListener(this);

	loginDescriptor.setValid(false);
	DialogDisplayer.getDefault().notify(loginDescriptor);
    }

    @Override
    public void logout()
    {
	SnpDataAPI.getDefault().disconnect();
	login = false;
	for(LoginEventListener listener : loginListeners)
	{
	    listener.onLogout();
	}
    }

    @Override
    public void addLoginEventListener(LoginEventListener listener)
    {
	loginListeners.add(listener);
    }

    @Override
    public void actionPerformed(ActionEvent event)
    {
	if(event.getSource() == DialogDescriptor.CANCEL_OPTION
		|| event.getActionCommand().equals("Escape"))
	{
	    loginDescriptor.setClosingOptions(null);
	} else
	{
	    loginPanel.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
	    try
	    {
		if(!SnpDataAPI.getDefault().connectAndAuth(loginPanel.getUsername(), loginPanel.getPassword()))
		{
		    loginPanel.setInfo("Błędny login lub hasło!");
		    loginPanel.setCursor(Cursor.getDefaultCursor());
		} else
		{
		    login = true;
		    for(LoginEventListener listener : loginListeners)
		    {
			listener.onLogin();
		    }
		    loginDescriptor.setClosingOptions(null);
		}

	    } catch(Exception e)
	    {
		loginPanel.setInfo("Serwer niedostępny!");
		loginPanel.setCursor(Cursor.getDefaultCursor());
		NotifyDescriptor nd = new NotifyDescriptor.Message(e.toString());
		DialogDisplayer.getDefault().notify(nd);
	    }
	}
    }

    @Override
    public void keyTyped(KeyEvent e)
    {
    }

    @Override
    public void keyPressed(KeyEvent e)
    {
    }

    @Override
    public void keyReleased(KeyEvent e)
    {
	if(loginPanel.jLoginField.getText().length() > 0
		&& loginPanel.jPasswordField.getPassword().length > 0)
	{
	    loginDescriptor.setValid(true);
	} else
	{
	    loginDescriptor.setValid(false);
	}
    }
}
